﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

/*
 * 六种请求方式
 * 1.post
 * 2.get
 * 3.head
 * 4.put
 * 5.delete
 * 6.upload
 * 
 * 要逐一实现
 * 注意点：
 * 1.要注意编码的问题 - 这是一个要严重问题
 */

namespace Ycg.Web
{
    //URI是URL的子集
    public class HttpClientHelper
    {
        public static void Get(string url)
        {
        }

        public static void Post(string url)
        {
        }

        private static void Request(string url, string requestMode)
        {
            if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("The url is null or empty.");

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            if (request != null)
            {
                request.Method = requestMode;
            }
        }
    }
}

/*
 namespace webclient
{
    class httpclient
    {
        private string cookie;
        public string Cookie
        {
            get { return cookie; }
            set { cookie = value; }
        }

        private string referer;
        public string Referer
        {
            get { return referer; }
            set { referer = value; }
        }

        /// <summary>
        ///  WEBCLIENT 方法处理http 当postdata为空  就使用GET，不为空就使用 POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postdata"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string webclient(string url, string postdata, string type)
        {
            string ret = "";
            WebClient myClient = new WebClient();
            myClient.Encoding = Encoding.GetEncoding(type);
            myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            myClient.Headers.Add("Cookie", cookie);
            myClient.Headers.Add("Referer", referer);
            if (postdata.Length == 0)
            {
                ret = myClient.DownloadString(url);
            }
            else
            {
                ret = myClient.UploadString(url, postdata);
            }            
            if (cookie.Length == 0 )
            {
                cookie = myClient.ResponseHeaders.Get("Set-Cookie");
            }
            return ret;
        }

        /// <summary>
        /// HttpWebRequest 方法处理http 当postdata为空  就使用GET，不为空就使用 POST
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postdata"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string HttpWebResponse(string url, string postdata, string type)
        {
            string ret = "";
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));           
            myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.AllowAutoRedirect = true;           
            //myHttpWebRequest.co
            string[] arrCookie = cookie.Split(';');
            CookieContainer cookie_container = new CookieContainer();    //加载Cookie            
            foreach (string sCookie in arrCookie)
            {
                if (sCookie.IndexOf("expires") > 0)
                    continue;
                cookie_container.SetCookies(new Uri(url), sCookie);
            }
            myHttpWebRequest.CookieContainer = cookie_container;
            myHttpWebRequest.Referer = referer;
            if (postdata.Length != 0)
            {
                myHttpWebRequest.Method = "POST";
                myHttpWebRequest.ContentLength = postdata.Length;
                Stream newStream = myHttpWebRequest.GetRequestStream();
                // Send the data.
                byte[] byteArray = Encoding.GetEncoding(type).GetBytes(postdata);
                newStream.Write(byteArray, 0, byteArray.Length);    //写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();                
                StreamReader str = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(type));
                ret = str.ReadToEnd();
                cookie = response.Headers.Get("Set-Cookie");
                str.Close();
                response.Close();
             
            }
            else
            {  
                myHttpWebRequest.Method = "GET";
                HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader sReader = new StreamReader(responseStream, Encoding.GetEncoding(type));
                ret = sReader.ReadToEnd();
                cookie = response.Headers.Get("Set-Cookie");
                sReader.Close();
                response.Close();
                
            }            
            
            
            return ret;
        }


    }
}
 */
